package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.sdklog.SdkLogPo;
import com.bwda.dsrs.filecenter.domain.condition.sdklog.SdkLogCondition;
import com.bwda.dsrs.filecenter.domain.form.sdklog.SdkLogQueryForm;
import com.bwda.dsrs.filecenter.domain.form.sdklog.SdkLogUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.sdklog.SdkLogCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.sdklog.SdkLogVo;
import com.bwda.dsrs.filecenter.service.SdkLogService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
import java.util.Date;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/sdklog")
@Api(value = "/b/sdklog", tags = "SDK日志表")
public class SdkLogController extends BaseController {

	@Autowired
	private SdkLogService sdkLogService;

	@ApiOperation(value = "查询SDK日志表",notes = "查询SDK日志表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<SdkLogVo> query(@ApiParam(value = "日志id", required = true)@RequestParam Integer logId) throws BusinessServiceException {
		SdkLogPo po = sdkLogService.queryWithValid(logId);
		SdkLogVo vo = CopyUtil.transfer(po, SdkLogVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询SDK日志表数量",notes = "查询SDK日志表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid SdkLogQueryForm form) throws BusinessServiceException {
		SdkLogCondition condition = this.getConditionByQueryForm(form);
		int count = sdkLogService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询SDK日志表列表",notes = "查询SDK日志表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<SdkLogVo>> queryList(@ModelAttribute@Valid SdkLogQueryForm form) throws BusinessServiceException {
		SdkLogCondition condition = this.getConditionByQueryForm(form);
		List<SdkLogPo> poList = sdkLogService.queryList(condition);
		List<SdkLogVo> voList = CopyUtil.transfer(poList, SdkLogVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询SDK日志表列表(带分页)",notes = "查询SDK日志表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<SdkLogVo>> queryPageList(@ModelAttribute@Valid SdkLogQueryForm form) throws BusinessServiceException {
		SdkLogCondition condition = this.getConditionByQueryForm(form);
		List<SdkLogVo> voList = new ArrayList<>();
		int count = sdkLogService.queryCount(condition);
		if (count > 0) {
			List<SdkLogPo> poList = sdkLogService.queryList(condition);
			voList = CopyUtil.transfer(poList, SdkLogVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增SDK日志表",notes = "新增SDK日志表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<SdkLogVo> add(@ModelAttribute@Valid SdkLogCreateForm form) throws BusinessServiceException {
		SdkLogPo po = CopyUtil.transfer(form, SdkLogPo.class);
//		po.setLogId(RandomUtils.nextInt());
		sdkLogService.insert(po);
		SdkLogVo vo = CopyUtil.transfer(po, SdkLogVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改SDK日志表",notes = "修改SDK日志表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid SdkLogUpdateForm form) throws BusinessServiceException {
		SdkLogPo po = CopyUtil.transfer(form, SdkLogPo.class);
		po.setOptTime(new Date());
		sdkLogService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除SDK日志表",notes = "删除SDK日志表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "日志id", required = true)@RequestParam Integer logId) throws BusinessServiceException {
		sdkLogService.delete(logId);
		return getSuccessResult();
	}

	/**
	 * SdkLogQueryForm转换为SdkLogCondition
	 *
	 * @param form
	 * @return
	 */
	private SdkLogCondition getConditionByQueryForm(SdkLogQueryForm form) {
		SdkLogCondition condition = CopyUtil.transfer(form, SdkLogCondition.class);
		return condition;
	}

}